<?php


namespace app\api\service;

use app\api\model\Course as CourseModel;
use app\api\model\CourseOrder as CourseOrderModel;
use think\Db;
use app\api\model\RemindOrder as RemindOrderModel;

class OrderRemindService
{
    public static function getOrders($status, $time)
    {
        return CourseOrderModel::where('status', $status)->where('createtime', '<=', $time)->select();
    }
    /**
     * 订单自动取消提醒
     */
    public static function autoCancel($time)
    {
        $courseOrders = self::getOrders(1, $time);
        Db::startTrans();
        try {
            foreach ($courseOrders as $val) {
                //更新订单状态
                $val->status = 0;
                $val->save();
                //更新课次状态
                CourseModel::whereIn('course_order_id', $val->course_order_id)->update(['status' => 0]);
                //取消提醒
                RemindOrderModel::add($val, 7);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        return isset($e) ? false : true;
    }

    /**
     * 超时未付款提醒
     */
    public static function waitPayment($time)
    {
        $courseOrders = self::getOrders(1, $time);
        Db::startTrans();
        try {
            foreach ($courseOrders as $val) {
                //待付款提醒
                RemindOrderModel::add($val, 1);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        return isset($e) ? false : true;
    }

}